package com.viewplug.listView;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;

/**
 * Created by mac_wk on 14-11-10.
 */
public class RefreshToSwapListView extends ListView {
    public RefreshToSwapListView(Context context) {
        super(context);
        init(context);
    }

    public RefreshToSwapListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public RefreshToSwapListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public RefreshToSwapListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context)
    {
    }
    private int lastX;
    private SwapListViewItem nowPressView;
    private SwapListViewItem nowShowView;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        switch (ev.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                lastX  = (int) ev.getX();
//                break;
//            case MotionEvent.ACTION_MOVE:
//                int pressX = (int) ev.getX();
//                if (Math.abs(pressX-lastX)>50) {
//                    return false;
//                }
//
//                break;
//        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                lastX  = (int) ev.getX();
                int position = pointToPosition((int)ev.getX(),(int)ev.getY());
                nowPressView = (SwapListViewItem) getChildAt(position-getFirstVisiblePosition());
                break;
            case MotionEvent.ACTION_MOVE:
                if(nowPressView==null)
                    break;
                int pressX = (int) ev.getX();
                if (Math.abs(pressX-lastX)>50)
                {
                    int x = (int) (ev.getX()-lastX);
                    int width = (int) (nowPressView.getMaxWdith());
//                    Log.e("message",width+"---x:"+x+"--");
                    //如果是右侧未滑动状态
                    if(x<0&&nowPressView.getNowState()==SwapListViewItem.right_state&&nowPressView.isHashRight())
                    {
                        if (Math.abs(x)<=width)
                        {
                            nowPressView.rightScroll((int) (x));
                            return true;
                        }
                        else
                            nowPressView.setNowState(SwapListViewItem.right_left_state);//证明滑动到了左边
                    }else if(x>0&&nowPressView.getNowState()==SwapListViewItem.right_left_state)
                    {

                        if (Math.abs(x)<=width)
                        {
                            nowPressView.rightScroll((int) (x));
                            return true;
                        }
                        else
                            nowPressView.setNowState(SwapListViewItem.right_state);//证明滑动到了右边
                    }
                }
                break;
            default:
                if (nowPressView!=null)
                nowPressView.scroller();
                break;
        }
        return super.onTouchEvent(ev);
    }


}
